package fun.sssdnsy.richtext;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;

public class DefaultContextMenu extends ContextMenu {
        private MenuItem fold, unfold;

        public DefaultContextMenu() {
            fold = new MenuItem("Fold selected text");
            fold.setOnAction(AE -> {
                hide();
                fold();
            });

            unfold = new MenuItem("Unfold from cursor");
            unfold.setOnAction(AE -> {
                hide();
                unfold();
            });

            getItems().addAll(fold, unfold);
        }

        /**
         * Folds multiple lines of selected text, only showing the first line and hiding the rest.
         */
        private void fold() {
            ((FoldableStyledArea) getOwnerNode()).foldSelectedParagraphs();
        }

        /**
         * Unfold the CURRENT line/paragraph if it has a fold.
         */
        private void unfold() {
            FoldableStyledArea area = (FoldableStyledArea) getOwnerNode();
            area.unfoldParagraphs(area.getCurrentParagraph());
        }
    }
